package pe.gob.osinergmin.dao.impl;

import android.content.Context;

import java.util.List;

import pe.gob.osinergmin.bean.SupervisionMovil;
import pe.gob.osinergmin.dao.SupervisionMovilDAO;
import pe.gob.osinergmin.utils.PuntoControlConstants;
import pe.gob.osinergmin.utils.PuntoControlUtils;

public class SupervisionMovilDAOImpl implements SupervisionMovilDAO {

    private static List<SupervisionMovil> memoryList;

    private static SupervisionMovilDAOImpl instance;

    private static Integer supervisionScopTotal = 0;
    private static Integer supervisionSeguridadTotal = 0;

    private static Integer supervisionScopPendiente = 0;
    private static Integer supervisionSeguridadPendiente = 0;

    @Override
    public List<SupervisionMovil> getSupervisionMovilList(Context context) throws Exception {
        if(memoryList == null){
            String json = PuntoControlUtils.readExternalFile(context, SupervisionMovil.class.getSimpleName());
            memoryList = (List<SupervisionMovil>) PuntoControlUtils.convertFromJsonStringToList(json);
        }
        return memoryList;
    }

    @Override
    public void saveSupervisionMovilListToLocal(Context context, List<SupervisionMovil> supervisionMovilList) throws Exception {
        String json = PuntoControlUtils.convertFromListToJsonString(supervisionMovilList);
        PuntoControlUtils.writeExternalFile(context, SupervisionMovil.class.getSimpleName(), json);
        memoryList = supervisionMovilList;
        processList(memoryList);
    }

    @Override
    public void deleteSupervisionMovilListFromLocal(Context context) throws Exception {
        PuntoControlUtils.deleteExternalFile(context, SupervisionMovil.class.getSimpleName());
        memoryList = null;
        processList(memoryList);
    }

    private void processList(List<SupervisionMovil> supervisionMovilList){
        supervisionScopTotal = 0;
        supervisionScopPendiente = 0;
        supervisionSeguridadTotal= 0;
        supervisionSeguridadPendiente = 0;
        if(supervisionMovilList != null){
            for(SupervisionMovil supervisionMovil : supervisionMovilList){
                if(PuntoControlConstants.tipoSupervision.SCOP.equals(supervisionMovil.getTipo())){
                    supervisionScopTotal++;
                    if(PuntoControlConstants.estadoSupervision.REGISTRADA.equals(supervisionMovil.getEstado())){
                        supervisionScopPendiente++;
                    }
                }
                if(PuntoControlConstants.tipoSupervision.SEGURIDAD.equals(supervisionMovil.getTipo())){
                    supervisionSeguridadTotal++;
                    if(PuntoControlConstants.estadoSupervision.REGISTRADA.equals(supervisionMovil.getEstado())){
                        supervisionSeguridadPendiente++;
                    }
                }
            }
        }else{
            try {
                List tList = getSupervisionMovilList(null);
                if(tList != null) {
                    processList(tList);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public Integer getSupervisionScopTotal(){
        return supervisionScopTotal;
    }

    public Integer getSupervisionSeguridadTotal(){
        return supervisionSeguridadTotal;
    }

    public Integer getSupervisionScopPendiente(){
        if(supervisionScopPendiente == 0) processList(memoryList);
        return supervisionScopPendiente;
    }

    public Integer getSupervisionSeguridadPendiente(){
        return supervisionSeguridadPendiente;
    }

    public static SupervisionMovilDAOImpl getInstance(){
        if(instance == null){
            instance = new SupervisionMovilDAOImpl();
        }
        return instance;
    }
}
